//
//  KKChatMsgBaseTableViewCell.m
//  ReplyAssistant
//
//  Created by liaozhenming on 2018/11/22.
//  Copyright © 2018 KK. All rights reserved.
//

#import "KKChatMsgBaseTableViewCell.h"

#import "KKMessageItem.h"
#import "RAMessage.h"
#import "RASender.h"

@interface KKChatMsgBaseTableViewCell(){
    UIImage *_in_backgroundImage;
    UIImage *_out_backgroundImage;
}

@end

@implementation KKChatMsgBaseTableViewCell

- (instancetype)initWithC2CReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier: reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _in_backgroundImage = [UIImage imageNamed: @"chat_bubble_in"];
        _out_backgroundImage = [UIImage imageNamed: @"chat_bubble_out"];
        
        _cellStyle = KKChatMsgBaseTableViewCellStyle_C2C;
        [self addC2CCellViews];
    }
    return self;
}

- (instancetype)initWithGroupReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier: reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _in_backgroundImage = [UIImage imageNamed: @"chat_bubble_in"];
        _out_backgroundImage = [UIImage imageNamed: @"chat_bubble_out"];
        
        _cellStyle = KKChatMsgBaseTableViewCellStyle_Group;
        [self addC2CCellViews];
    }
    return self;
}

- (void)dealloc {
    
}

#pragma mark - Override methods
- (void)layoutSubviews {
    [super layoutSubviews];
    [self relayoutFrameOfSubViews];
}

#pragma mark - Private methods
- (void)relayoutFrameOfSubViews {
    switch (_cellStyle) {
        case KKChatMsgBaseTableViewCellStyle_C2C:
        {
            [self relayoutC2CCellViews];
        }
            break;
        case KKChatMsgBaseTableViewCellStyle_Group:
        {
            [self relayoutGroupCellViews];
        }
            break;
            
        default:
            break;
    }
}

- (void)relayoutC2CCellViews {
    //  重新布局C2C类型的cell
    CGRect rect = self.contentView.bounds;
    
    CGSize contentSize = [self.msg contentSizeInWidth:rect.size.width];
    
//    CGSize contentBackSize = CGSizeMake(contentSize.width + 35, contentSize.height + 10);
    CGSize contentBackSize = CGSizeMake(contentSize.width + 20, contentSize.height + 20);
    
    if (!self.msg.isMineMsg) {
        //  接收到的消息
        _img_userHeader.frame = CGRectMake(10, 10, 36, 36);
        
        _img_contentBackground.frame = CGRectMake(CGRectGetMaxX(_img_userHeader.frame) + 10, 10, contentBackSize.width, contentBackSize.height);
        
        _img_contentBackground.backgroundColor = [UIColor whiteColor];
        _msgContent.frame = CGRectMake(10, 10, contentSize.width, contentSize.height);
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_img_contentBackground.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight| UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _img_contentBackground.bounds;
        maskLayer.path = maskPath.CGPath;
        _img_contentBackground.layer.mask = maskLayer;
    }
    else {
        //  自己发送的消息
        _img_userHeader.frame = CGRectMake(CGRectGetWidth(rect) - 46, 10, 36, 36);
        
        _img_contentBackground.frame = CGRectMake(CGRectGetMinX(_img_userHeader.frame) - 10 - contentBackSize.width, 10, contentBackSize.width, contentBackSize.height);
//        [_img_contentBackground setImage:[_out_backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(25.0f,30.0f,15.0f,20.0f) resizingMode:UIImageResizingModeStretch]];
        
        _img_contentBackground.backgroundColor = [UIColor colorWithHexString: @"74a0e3"];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_img_contentBackground.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight| UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _img_contentBackground.bounds;
        maskLayer.path = maskPath.CGPath;
        _img_contentBackground.layer.mask = maskLayer;
        
        
        _msgContent.frame = CGRectMake(10, 10, contentSize.width, contentSize.height);
        
//        _sendTipView.frame = CGRectMake(CGRectGetMinX(_img_contentBackground.frame) - 35, CGRectGetMaxY(_img_contentBackground.frame) - CGRectGetHeight(_img_contentBackground.bounds)/2 - 15, 30, 30);
//        _sendTipView.hidden = false;
    }
}

- (void)relayoutGroupCellViews {
    //  重新布局group类型的cell
}

#pragma mark - Public methods

- (void)configWithMsg:(KKMessageItem*)msg {
    if (self.msg != msg) {
        self.msg = msg;
    }
    [self configContent];
}

//  配置消息内容
- (void)configContent {
    
    RAMessage *sender = self.msg.msg;
    NSString *headImag = sender.senderHeadImg;
    if ([headImag isKindOfClass: [NSString class]]) {
        
        NSString *headimg = sender.senderHeadImg;
        if (![headimg hasPrefix: @"http"]) {
            headimg = [NSString stringWithFormat: @"%@%@",APP_Request_BasicURLString,headimg];
        }
        [_img_userHeader setImageWithURL: [NSURL URLWithString: headimg] placeholderImage: nil];
    }
}

// 添加C2C样式下的控件
- (void)addC2CCellViews {
    _img_userHeader = [UIImageView new];
//    _img_userHeader.layer.cornerRadius = 18.0;
//    _img_userHeader.layer.masksToBounds = true;
//    _img_userHeader.userInteractionEnabled = true;
    [self.contentView addSubview: _img_userHeader];
    
    _img_contentBackground = [[UIImageView alloc] init];
    _img_contentBackground.userInteractionEnabled = YES;
//    _img_contentBackground.layer.cornerRadius = 5;
    [self.contentView addSubview: _img_contentBackground];
    
    _msgContent = [self addElemContent];
    if (_msgContent) {
//        _msgContent.userInteractionEnabled = true;
        [_img_contentBackground addSubview: _msgContent];
    }
}

- (UIView*)addElemContent {
    return nil;
}

@end
